//
//  ComposePictureview.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/27.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

import SVProgressHUD

let KpictureMagin:CGFloat = 5

class ComposePictureview: UICollectionView {
    
    var imageList:[UIImage]? = [] {
        didSet{
            //判断图片数量
            if (imageList?.count ?? 0) > 0{
                self.hidden = false
            }else{
                self.hidden = true
            }
        }
    }
    
    var selectedPictureClosure:(() -> ())?
    
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        super.init(frame: frame, collectionViewLayout: layout)
        self.setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    //MARK: -  布局
    func setupUI(){
        
//        let image = UIImage(named: "compose_pic_add")
//        
//        imageList?.append(image!)
        
        self.hidden = true
        self.dataSource = self
        self.delegate = self
        self.backgroundColor = UIColor.whiteColor()
        self.registerClass(ComposePictureCell.self, forCellWithReuseIdentifier: "cell")
        
        
        //设置layout
        let layout = self.collectionViewLayout as! UICollectionViewFlowLayout
        let width = (KscreenWidth - 2 * KpictureMagin) / 3
        layout.itemSize = CGSize(width: width, height: width)
        layout.minimumInteritemSpacing = KpictureMagin
        layout.minimumLineSpacing = KpictureMagin
    }
    
    func addImage(image: UIImage) {
        if (imageList?.count ?? 0) < 9 {
            imageList!.append(image)
            reloadData()
        }else{
            SVProgressHUD.showErrorWithStatus("数量已达上限")
        }
    }

    
    
    

}




extension ComposePictureview:UICollectionViewDataSource,UICollectionViewDelegate{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let count = (imageList?.count ?? 0)
        return (count > 8 || count == 0) ? count : (count + 1)
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! ComposePictureCell
        if indexPath.row == imageList?.count {
            cell.image = nil
        }else{
            cell.image = imageList![indexPath.row]
            
            cell.delete = {[weak self] in
                self?.imageList!.removeAtIndex(indexPath.item)
                self?.reloadData()
            }

        }
        
                return cell
    }
    
    //添加选中事件
    func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {

        if indexPath.row == imageList?.count{
            //打开图片选择器
            selectedPictureClosure!()
        }
    }
    
    
}
